import * as axios from '@/api/home.js'
import { withMsg2 } from '@/utils/request.js'

export default {
  // 模块必须有这句话
  namespaced: true,
  state: {
    channel: [],
    news: [],
    newdate: ''
  },
  mutations: {
    setinfo(state, info) {
      state.channel = info
    },
    getnews(state, info) {
      state.news = info
    },
    addnews(state, info) {
      state.news.push(...info)
    },
    getnewdate(state, info) {
      state.newdate = info
    }
  },
  actions: {
    async getinfo(context) {
      const { data: res } = await withMsg2(axios.channels())
      context.commit('setinfo', res.data.channels)
    },
    async getnews(context, aaa) {
      const { data: res } = await withMsg2(axios.articles(aaa))
      console.log(res)
      context.commit('getnews', res.data.results)
      context.commit('getnewdate', res.data.pre_timestamp)
    },
    async addnews(context, aaa) {
      const { data: res } = await withMsg2(axios.articles(aaa))
      console.log(res)
      context.commit('addnews', res.data.results)
      context.commit('getnewdate', res.data.pre_timestamp)
    }
  }
}
